import { request } from '@inbiz/utils'

// 获取菜单导航
export async function getMenuNavigation(parentId: string) {
  return request(
    `/inbiz/wcm/portal/default/Service?id=siteMapTree&webSiteID=22a0a47768b74d04b7a2e89141a8115f&virtualPath=/inbiz/wcm/aaa&includePage=true&dataVersionId=05529510c4b54009a81bf059e84493a1&parentID=${parentId}&_=1646816526947`
  )
}

// 文档的历史搜索、联想词
export function GetEcmHistorySearch(params:
  { keyword: string; type: number; curFolderId: number; searchLocation: string }
) {
  return request('/WebCore', {
    method: 'POST',
    requestType: 'form',
    data: {
      module: 'WebClient',
      fun: 'GetHotWordsList',
      keyword: params.keyword, // 关键字
      type: params.type, //  获取ecm搜索类型   0:搜索文件和内容   1:内容    2:文件  4:文件夹名  3:标签
      curFolderId: params.curFolderId,
      searchLocation: params.searchLocation, // 获取Ecm获取搜索企业库、团队库、个人库  根据文件路径进行判断
    }
  })
}

// 删除Ecm搜索历史记录
export function deleteSearchHistory() {
  return request('/WebCore', {
    method: 'POST',
    requestType: 'form',
    data: {
      module: 'WebClient',
      fun: 'DelHotWordsList',
    }
  })
}

// 获取文档搜索 所有标签数据(ecm的文件打了标签的)
export function getTagListData() {
  return request('/WebCore', {
    method: 'POST',
    requestType: 'form',
    data: {
      module: 'WebClient',
      fun: 'GetTagList',
    }
  })
}

// 获取文档搜索 所有标签数据(ecm的文件打了标签的)
export function getCurrentUserTagList() {
  return request('/WebCore', {
    method: 'POST',
    requestType: 'form',
    data: {
      module: 'WebClient',
      fun: 'GetCurrentUserTagList',
    }
  })
}
// 全部 获取历史搜索关键字
export function getSearchHistory(params: { ModuleName: string, Size: number }): Promise<{
  Data: string[]
}> {
  return request('/search/IndexSearch/GetSearchHistoryByModuleName', {
    method: 'POST',
    requestType: 'form',
    data: {
      Size: params.Size,
    }
  })
}

// 获取热门搜索关键字
export function fetchSearchKeyHotWord({ Size = 8 }: { Size?: number }): Promise<string[]> {
  return request('/search/IndexSearch/GetSearchKeyHotWord', {
    method: 'POST',
    requestType: 'form',
    data: {
      Size,
    }
  })
}

// 获取联想词
export function getSynonymWord(params: { IndexName: string, KeyWord: string, Size: number, SearchWhere: string }): Promise<string[]> {
  return request('/search/IndexSearch/GetSearchKeyWordData', {
    method: 'POST',
    requestType: 'form',
    data: {
      IndexName: params.IndexName, // 索引库名
      KeyWord: params.KeyWord, // 关键字
      Size: params.Size,
      SearchWhere: params.SearchWhere,
    }
  }).then((res: { Data: any; }) => res.Data || [])
}

export type ITag = {
  Id: string;
  LabelName: string;
  LabelValue: string;
  CreateTime: string;
}
// 我的搜索标签（文件、文件夹高级搜索打的标签）
export function SearchLabelManager(ModuleName: string): Promise<ITag[]> {
  return request.post('/search/IndexSearchLabel/GetSearchLabelList', {
    requestType: 'form',
    data: {
      ModuleName
    }
  }).then((res: { Data: any; }) => res.Data || [])
}


// 编辑标签  我的搜索标签（文件、文件夹高级搜索打的标签） 
export function editLabelName(params: {
  Id: string,
  ModuleName: string;
  LabelName: string;
  labelValue: string
}) {
  return request.post('/search/IndexSearchLabel/Update', {
    requestType: 'form',
    data: params
  })
}

// 编辑标签  我的搜索标签（文件、文件夹高级搜索打的标签） 
// export function editLabelValue(labelId: string, labelValue: string) {
//   const params: Record<string, string> = {
//     module: 'SearchLabelManager',
//     fun: 'EditValue',
//     labelId,
//     labelValue,
//   };
//   return request('/WebCore', {
//     method: 'POST',
//     requestType: 'form',
//     data: params,
//   })
// }

/**
 * 添加  我的搜索标签（文件、文件夹高级搜索打的标签） 
 * @param labelName 标签名称
 * @param labelValue 表单内容
 *  ModuleName
 * @returns 
 */
export function addLabelName(params: { LabelName: string, LabelValue: string, ModuleName: string }) {

  // return request('/WebCore', {
  //   method: 'POST',
  //   requestType: 'form',
  //   data: {
  //     module: 'SearchLabelManager',
  //     fun: 'Add',
  //     labelName,
  //     labelValue,
  //     type,
  //   }
  // })
  return request.post('/search/IndexSearchLabel/Add', {
    requestType: 'form',
    data: params
  })
}

// 删除标签  我的搜索标签（文件、文件夹高级搜索打的标签） 
export function deleteLabelName(labelId: string) {
  return request.post('/search/IndexSearchLabel/Delete', {
    requestType: 'form',
    data: {
      labelId
    }
  })
}

// 获取元数据类别
export async function getMetaTypeList() {
  return request(
    `/inbiz/eform/metadataapi/GetMetaTypeList?isFillMetaAttr=true`
  )
}


// 删除单个历史搜索记录
export function deleteSingleSearchHistory(delWord: string) {
  return request('/search/IndexSearch/DeleteSingleSearchHistory', {
    method: 'POST',
    requestType: 'form',
    data: {
      delWord: delWord,
    }
  })
}

// 档案 删除全部历史搜索记录
export function deleteSearchHistoryByModule(ModuleName: string) {
  return request('/search/IndexSearch/DeleteSearchHistoryByModule', {
    method: 'POST',
    requestType: 'form',
    // data: {
    //   ModuleName: ModuleName
    // }
  })
}

export const loadPageDetails = (globalSDK?: any, pageId?: string) => {
  return request("/inbiz/api/services/front/engines/v" + InbizConfig.API_VERSION + "/page/loadDetail", {
    siteOutId: globalSDK.appInfo.OutId || globalSDK.appInfo.outId,
    //@ts-ignore
    dataVersionId: globalSDK.appInfo.DataVersionId || globalSDK.appInfo.dataVersionId,
    outId: pageId,
  })
}

export async function UpdateTemplateFile(
  appID: string,
  file: File,
): Promise<{
  fileName: string;
  path: string;
}> {
  const formData = new FormData();
  formData.append('file', file);
  formData.append('groupName', 'template');

  return request(`/inbiz/api/services/staticresource/v3/file/system/${appID}`, {
    method: 'POST',
    data: formData,
  });
}
